<template>
	<view>
		<view class="sticky-box">
			<search-input ref="searchInput" @searchConfirm="handleSearch($event)"
				:isHideKeyboard="false"></search-input>
		</view>
		<view class="goods-list">
			<view class="goods-item" v-for="(item, index) in goodsList" :key="index" @click="duima(item)">
				<view class="flex-row goods-header">
					<view class="goods-name">
						<text>{{item.Name}}</text>
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						商品编码：{{item.Code}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						国药准字：{{item.ApprovalNumber}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.Manufacturer}}
					</view>
				</view>
				<view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.PackageSpec}}/{{item.Unit}}
					</view>
				</view>
				<!-- <view class="flex-row goods-row">
					<view class="goods-unit">
						{{item.ApprovalNumber}}
					</view>
				</view> -->
			</view>
		</view>


		<uni-popup ref="popup" type="center" borderRadius="20rpx 20rpx 20rpx 20rpx">
			<view class="update-PKG">
				<view class="pkg-title">对码</view>
				<view class="pkg-panel">
					<view class="flex-row box-row pkg-item">
						<view class="left">溯源码</view>
						<view class="right">
							{{SupervisionCode}}
						</view>
					</view>
					<view class="flex-row box-row pkg-item">
						<view class="left">商品名称</view>
						<view class="right">
							{{product.Name}}
						</view>
					</view>
					<view class="flex-row box-row pkg-item">
						<view class="left">商品编号</view>
						<view class="right">
							{{product.Code}}
						</view>
					</view>					
					<view class="flex-row box-row pkg-item">
						<view class="left">医院入价</view>
						<view class="right">
							{{product.Price}}
						</view>
					</view>
					<view class="flex-row box-row pkg-item" v-if="product.Manufacturer">
						<view class="left">生产厂家</view>
						<view class="right showFacture">
							{{product.Manufacturer}}
						</view>
					</view>
					<view class="flex-row box-row pkg-item">
						<view class="left">医院入库单位</view>
						<view class="right">
							<picker @change="bindPickerChange" :value="deliveryIndex" :range="units">
								<view class="flex-row pkg-picker">
									<view class="uni-input">{{units[deliveryIndex]}}</view>
									<text class="picker-icon">▼</text>
								</view>
							</picker>

						</view>
					</view>
					<view class="flex-row box-row pkg-item" v-if="product.DeliveryUnit">
						<view class="left">配企出库单位</view>
						<view class="right">
							<picker @change="bindDeliveryChange" :value="index" :range="units">
								<view class="flex-row pkg-picker">
									<view class="uni-input">{{units[index]}}</view>
									<text class="picker-icon">▼</text>
								</view>
							</picker>

						</view>
					</view>
					<view class="flex-row box-row pkg-item">
						<view class="left">追溯单位比例</view>
						<view class="right" style="margin-left: 10px; flex: 1;padding-left: 10rpx;"><input type="digit"
								class="pkg-input" placeholder="追溯单位比例" v-model="Pkg_Amount" /></view>
					</view>
					<block  v-if="$Route.query.scanType == '2'">
						<view class="flex-row box-row pkg-header">
							<view class="header-title" style="margin-right: 10px;">配企单位比例</view>
							<view class="header-title">医院入库比例</view>
						</view>
						<view class="flex-row box-row pkg-input-panel">
							<view class="flex-row"  style="margin-right: 20px;">
								<input type="digit" class="pkg-input-item" placeholder="配企单位比例" v-model="DeliveryRatio" />
								<text style="margin-left: 10rpx;">{{units[index]}}</text>
							</view>
							<view class="flex-row">
								<input type="digit" class="pkg-input-item" placeholder="医院入库比例" v-model="WarehousePackageRatio" />
								<text style="margin-left: 10rpx;">{{units[deliveryIndex]}}</text>
							</view>
						</view>
					</block>
					

					<view class="pkg-btn flex-row box-row">
						<view class="btn cencel" @click="closePKG">取消</view>
						<view class="btn confirm" @click="checkCode">确认</view>
					</view>
				</view>

			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	var that;
	export default {
		data() {
			return {
				searchVal: '',
				product: {}, //对码商品
				index: 0,
				deliveryIndex:0,
				Pkg_Amount: 1,
				TransRate: 1,
				SupplierCode: '',
				SupervisionCode: '',
				DeliveryRatio:1, //配企单位比例
				WarehousePackageRatio:1,//医院单位比例
				goodsList: [],
				units: ['盒', '箱', '件'],
				showGoodsList: [],
				activeIndex: -1,
				start: 0,
				step: 20,
				page: 1,
			}
		},
		computed: {
			...mapState({
				userInfo: state => state.userInfo, // userInfo
				Token: state => state.Token, // Token
			})
		},

		onLoad() {
			that = this
			that.SupplierCode = that.$Route.query.SupplierCode;
			that.SupervisionCode = that.$Route.query.SupervisionCode;
			that.getServerData()
		},
		onShow() {
			that.getUnitList();

		},
		onReachBottom() {
			that.page++;
			that.getGoodsList()
		},
		methods: {
			...mapMutations(['initScanCode', 'cleanScanCode', 'initCompanyGood', 'initCompanyGoods']),
			getServerData() {
				that.getGoodsList();
			},
			bindPickerChange(e) {
				that.deliveryIndex = e.detail.value;
			},
			bindDeliveryChange(e){
				that.index = e.detail.value;
			},
			//获取单位列表
			getUnitList() {
				that.$request.globalRequest('goods/unitlist', 'POST', {}, '', 0).then(res => {
					that.units = res;
				}).catch(res => {
					that.$common.showNormalToast(res);
				})
			},
			//关闭对码
			closePKG() {
				that.$refs.popup.close()
			},
			//打开对码
			duima(item) {
				that.$refs.popup.open();
				that.product = item;
				if (!that.product.DeliveryUnit || that.product.DeliveryUnit.length<=0)
				{
					that.product.DeliveryUnit = that.units;
				}
			
				that.units.forEach((u, index) => {
					if (u == item.WarehouseUnit) {
						that.deliveryIndex = index
					}	
				})
				
				if (that.$Route.query.scanType == "2")
				{
					that.WarehousePackageRatio = that.product.WarehousePackageRatio;
					that.DeliveryRatio = that.product.DeliveryRatio;
					that.Pkg_Amount = that.product.UnitRatio;
				}
				
				let deliveryUnit = ''
				if (typeof that.product.DeliveryUnit == "string")
				{
					deliveryUnit = that.product.DeliveryUnit;
				}
				else if (that.product.DeliveryUnit.length>0){
					deliveryUnit = that.product.DeliveryUnit[0];
				}	
				that.units.forEach((u, index) => {
					if (u == deliveryUnit.trim()) {
						that.index = index
					}
				})
				
			},
			//对码
			checkCode() {
				let requestData = {
					Goods_CodeId: that.product.Code,
					PackageRatio: that.Pkg_Amount,
					Unit: that.units[that.index],
					WarehouseRatio:that.WarehousePackageRatio,
					WarehouseUnit:that.units[that.deliveryIndex],
					DeliveryRatio:that.DeliveryRatio,
					Code: that.SupervisionCode
				}
				//console.log(requestData)
				that.$request.globalRequest('goods/check', 'POST', requestData, '', 0).then(res => {
					uni.hideLoading();
					that.$refs.popup.close();
					//console.log(that.$Route.query);
					if (that.$Route.query.scanType && that.$Route.query.scanType == "1") {

						//如果从商品对码进来，则返回商品对码界面
						// let url = {
						// 	path: '/pagesContrast/goodsContrast/goodsContrast', 
						// 	query: {
						// 		SupplierCode: that.$Route.query.SupplierCode,
						// 	}
						// }
						//that.$Router.replace(url)
						that.$Router.back();
					} else if (that.$Route.query.scanType && that.$Route.query.scanType == "3") {
						//如果是扫码退厂
						let companyTuiGoods = uni.getStorageSync('companyTuiGoodsList') || [];
						companyTuiGoods.forEach(item => {
							if (item.Code_His && item.Code_His == that.product.Code) {
								if (item.GoodsCheckCodeList) {
									var newCheckCode = {
										Code: item.Code_His,
										SupervisionCode: that.SupervisionCode,
										UnitRatio: that.Pkg_Amount										
									}
									item.currentCheckCode = newCheckCode;
									item.WarehousePackageRatio = that.WarehousePackageRatio;
									item.DeliveryRatio = that.DeliveryRatio;
									item.GoodsCheckCodeList.push(newCheckCode)
								}
								
								item.Unit = that.units[that.index];
								item.WarehouseUnit = that.units[that.deliveryIndex]
								//如果只匹配一个商品，则直接跳转到详情页
								that.initCompanyTuiGood(item);
								let url = {
									path: '/pagesStockOut/goodsDetail/goodsDetail'
								}
								that.initCompanyGoods(companyTuiGoods);
								that.$Router.replace(url)
							}

						});
					} else if (that.$Route.query.scanType && that.$Route.query.scanType == "2") {
						//如果从溯源扫码界面进来则返回溯源扫码入库详情界面
						let companyGoods = uni.getStorageSync('companyGoodsList') || [];

						companyGoods.forEach(item => {
							if (item.Code_His && item.Code_His == that.product.Code) {
								if (item.GoodsCheckCodeList) {
									var newCheckCode = {
										Code: item.Code_His,
										SupervisionCode: that.SupervisionCode,
										UnitRatio: that.Pkg_Amount,									
										
									}
									item.currentCheckCode = newCheckCode;
									item.WarehousePackageRatio = that.WarehousePackageRatio;
									item.DeliveryRatio = that.DeliveryRatio;
									item.GoodsCheckCodeList.push(newCheckCode);									
								}
								item.Unit = that.units[that.index];
								item.DeliveryUnit = that.units[that.deliveryIndex]
								//如果只匹配一个商品，则直接跳转到详情页
								that.initCompanyGood(item);
								let url = {
									path: '/pagesStockIn/goodsDetail/goodsDetail'
								}
								that.initCompanyGoods(companyGoods);
								//console.log(companyGoods,that.product.Code)
								that.$Router.replace(url)
							}
						});
					} else if (that.$Route.query.scanType && that.$Route.query.scanType == "4") {

						let url = {
							path: '/pages/cailing/cailing',
							query: {
								SupervisionCode: that.SupervisionCode,
								PharmacyCode: that.$Route.query.PharmacyCode
							}
						}
						that.$Router.replace(url)

					}

				}).catch(res => {
					uni.hideLoading();
					console.log(res)
				})
			},
			//商品列表
			getGoodsList() {
				//console.log('aaaa')
				let query = {
					SupplierCode: '',
					SupervisionCode: '',
					keyword: that.searchVal,
					DateSpan: 15,
					sidx: "DeliveryTime desc"
				}
				let requestData = {
					pagination: {
						rows: 20,
						page: that.page
					},
					queryJson: JSON.stringify(query)
				};

				uni.showLoading({
					title: '加载中'
				});
				//默认从任务列表取数据
				var url = "supplierstock/stockout";
				//如果从对码界面进来则取基础表的数据
				if (that.$Route.query.scanType == "1" || that.$Route.query.scanType == "4") {
					url = "goods/checklist";
				}
				//console.log(requestData)
				that.$request.globalRequest(url, 'POST', requestData, '', 0).then(res => {
					uni.hideLoading();
					//console.log(res)
					let data;
					if (that.$Route.query.scanType == "1" || that.$Route.query.scanType == "4") {
						data = res
					} else {
						data = res.map(item => {
							let newItem = item;
							newItem.Code = item.Code_His;
							newItem.ApprovalNumber = item.RegistrationCode;
							newItem.WarehousePackageRatio = item.WarehousePackageRatio;
							newItem.PackageRatio = item.UnitRatio?item.UnitRatio:1;
							newItem.WarehouseUnit = item.WarehouseUnit;
							newItem.PackageSpec = item.Spec;
							return newItem;
						})
					}
					//console.log(data)
					if (that.page == 1) {
						that.goodsList = data;
					} else {
						that.goodsList = that.goodsList.concat(data)
					}
					//that.page = 1
					//that.showGoodsList = [];
					//that.initShowGoodsList()

				}).catch(res => {
					uni.hideLoading();
					console.log(res)
				})
			},
			initShowGoodsList() {
				let start = that.step * that.page
				let end = that.step * ++that.page
				that.showGoodsList = that.showGoodsList.concat(that.goodsList.slice(start, end))
			},
			handleSearch(res) {
				that.searchVal = res.value
				that.page = 1;
				that.getGoodsList()
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "uncheckGoodsList.scss"
</style>